package com.cysion.study.ui.fragment

import com.cysion.study.R
import com.cysion.study.entity.ContactBean
import com.cysion.study.entity.TongxunEntity
import com.cysion.study.helper.UserCache
import com.cysion.study.ktbox.base.BaseFragment
import com.cysion.study.ktbox.net.BaseResponseRx
import com.cysion.study.ktbox.net.ErrorHandler
import com.cysion.study.net.ApiClient
import com.cysion.study.uibox.bar.TopBar
import com.cysion.study.uibox.dialog.Alert
import com.cysion.study.widget.ShowToast
import com.cysion.study.widget.indexableBar.AddressUser
import com.cysion.study.widget.indexableBar.SideBar
import com.cysion.study.widget.indexableBar.SortAdapter
import com.cysion.targetfun._subscribe
import com.scwang.smartrefresh.layout.constant.RefreshState
import kotlinx.android.synthetic.main.fragment_tongxun.*
import java.util.*

/**
 * 普通教师-通讯录
 * Created by chu on 2019-06-30.
 */
class AddressBookFragment:BaseFragment(){
//    private lateinit var classAdapter: AddressBookAdapter
    private var simpleDatas: MutableList<ContactBean> = mutableListOf()
    private var contacts: MutableList<AddressUser> = mutableListOf()

//    private lateinit var contactsAdapter: ContactsAdapter

    override fun getLayoutId(): Int {
        return R.layout.fragment_tongxun
    }

    override fun initView() {
//        darkTextTheme(color(R.color.main_cl))
        topbar.apply {
            initElements(TopBar.ELEMENT.HIDE,TopBar.ELEMENT.TEXT,TopBar.ELEMENT.HIDE)
            setTitle("通讯录")
        }
        initRecyclerView()
        initRefreshLayout()
    }

    //    初始化刷新控件
    private fun initRefreshLayout() {
        smartLayout.setEnableLoadMore(false)
        smartLayout.setOnRefreshListener {
            getDataList()
        }
    }

    override fun initData() {
        super.initData()
        smartLayout.autoRefresh()
    }

    //初始化列表
    private fun initRecyclerView() {

        side_bar.setOnStrSelectCallBack(object : SideBar.ISideBarSelectCallBack {
            override fun onSelectStr(index: Int, selectStr: String) {
                for (i in contacts.indices) {
                    if (selectStr.equals(contacts.get(i).getFirstLetter(), ignoreCase = true)) {
                        listView.setSelection(i) // 选择到首字母出现的位置
                        return
                    }
                }
            }
        })
        //设置缩放字体的个数，即开口大小  缩放个数item，即开口大小 默认6
        side_bar.setScaleItemCount(4)
        //设置字体缩放比例  缩放的倍数 默认1
        side_bar.setScaleSize(3)
        side_bar.setScaleWidth(60)
        //设置索引目录
//        side_bar.setDataResource()
    }


    private var startRefresh=true
    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        if (isVisibleToUser) {
            getDataList()
            if (startRefresh) {
                startRefresh=false
            }
        }
    }

    private val dataList: MutableList<TongxunEntity> = mutableListOf()
    private var name=""
    private fun getDataList() {
        contacts.clear()
//        Alert.loading(context)
        ApiClient.api.getAddressBook(name,UserCache.token).compose(BaseResponseRx.validateToMain())
            ._subscribe {
                _onNext {
//                    Alert.close()
                    stopRefresh()


                    if (it == null || it.size == 0) {
                        multiView.showEmpty()
                    } else {
                        multiView.showContent()
                    }

                    for (item in it) {
//                        var user:AddressUser
                        contacts.add(AddressUser(item.name))
                    }


//                    contacts.addAll(it)
                    println("###通讯录数据="+contacts.size)
                    contacts.sort() // 对list进行排序，需要让User实现Comparable接口重写compareTo方法
                    val adapter = SortAdapter(context, contacts)
                    listView.adapter = adapter
                    adapter.notifyDataSetChanged()
                    /*if (it.size == 0) {
                        multiView.showEmpty()
                    } else {
                        simpleAdaper.setDatas(simpleDatas)
                        simpleAdaper.notifyDataSetChanged()
                        multiView.showContent()
                    }*/

                }
                _onError {
                    stopRefresh()
//                    Alert.close()
                    ShowToast(ErrorHandler.handle(it).errorMsg)
                }
            }
    }

    private fun stopRefresh() {
        if (smartLayout.state == RefreshState.Refreshing) {
            smartLayout.finishRefresh()
        } else if (smartLayout.state == RefreshState.Loading) {
            smartLayout.finishLoadMore(100)
        }
    }




    override fun closeMvp() {
    }
}